using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web;
using System.Xml;
using System.Web.UI;
using System.Diagnostics;
using System.Xml.Xsl;

namespace IWCommunity.Blogs.WebControls
{
    public class XmlBasedControl : SPControl, IXmlDocumentDataSource
    {
        private XslTransformation _transform;
        private XmlDocument _document;
        private string _xmlDataSource;

        public string XmlDataSource
        {
            get { return _xmlDataSource; }
            set { _xmlDataSource = value; }
        }

        public XmlDocument Document
        {
            get 
            {
                if (_document != null) return _document;

                if (!string.IsNullOrEmpty(XmlDataSource))
                    _document = GetDataSourceDocument();
                else
                    _document = BuildDocument();

                return _document; 
            }
            set { _document = value; }
        }

        protected virtual XmlDocument BuildDocument() { return null; }

        protected virtual XmlDocument GetDataSourceDocument()
        {
            XmlDocument oError = new XmlDocument();
            oError.LoadXml("<Error/>");

            try
            {
                if (XmlDataSource == ID) return BuildDocument();

                Control c = NamingContainer.FindControl(XmlDataSource);

                if (c == null)
                    oError.InnerText = "XmlDataSource not found";
                else if (c is IXmlDocumentDataSource)
                    return ((IXmlDocumentDataSource)c).Document;
                else
                    oError.InnerText = "Invalid XmlDataSource";
            }
            catch (Exception ex)
            {
                Trace.Write(ex);
                oError.InnerText = ex.Message;
            }

            return oError;
        }

        public XslTransformation Transform
        {
            get { if (_transform == null) _transform = new XslTransformation(); return _transform; }
            set { _transform = value; }
        }

        private int _cacheTimeout = 2; //set default to 2 minutes
        public int CacheTimeout
        {
            get { return _cacheTimeout; }
            set { _cacheTimeout = value; }
        }

        private bool _enableCaching = false;
        public bool EnableCaching
        {
            get { return _enableCaching; }
            set { _enableCaching = value; }
        }

        private bool _cacheDebug = false;
        public bool CacheDebug
        {
            get { return _cacheDebug; }
            set { _cacheDebug = value; }
        }

        protected SPWeb Web { get { return SPContext.Current.Web; } }

        //protected string ThemeName
        //{
        //    get { return BlogContext.Current.BlogTheme; }
        //}

        //protected string ThemeVariation
        //{
        //    get { return BlogContext.Current.BlogThemeVariation; }
        //}

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            Transform.CacheID = UniqueID + "_" + Web.Url;
            Transform.CacheTimeout = _cacheTimeout;
            Transform.CachePerPage = false;
            Transform.CacheHtml = _enableCaching;
            Transform.CacheDebug = _cacheDebug;

            if (!Transform.IsHtmlCached || !_enableCaching) //no point getting document if we are going to render html from cache
                Transform.Document = Document;
            
            Transform.RenderControl(writer);
        }

        public static XmlDocument CleanXml(string xml)
        {
            XmlDocument oDoc = new XmlDocument();
            oDoc.LoadXml(xml);
            CleanXml(oDoc);
            return oDoc;
        }

        public static void CleanXml(XmlDocument xml)
        {
            XmlTextReader oReader = new XmlTextReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("IWCommunity.Blogs.WebControls.Resources.CleanXml.xsl"));
            XslCompiledTransform oTransform = new XslCompiledTransform();
            oTransform.Load(oReader);

            System.IO.StringWriter sw = new System.IO.StringWriter();

            XmlTextWriter writer = new XmlTextWriter(sw);
            oTransform.Transform(xml, null, writer);
            xml.LoadXml(sw.ToString());
        }

    }
}
